package com.zy.service.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.dao.sys.CityDao;
import com.zy.entity.sys.City;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.sys.CityService;
import com.zy.utils.tree.AreaTree;
import com.zy.utils.tree.BaseTree;
import com.zy.utils.tree.TreeUtil;

@Service("cityService")
public class CityServiceImpl extends BaseServiceImpl<City> implements CityService {

	@Autowired
	private CityDao cityDao;


	@Override
	public BaseTree cityTree() {

		BaseTree root = new BaseTree();
		root.setId("0");
		root.setParentId("-1");
		List<AreaTree> rootNode = new ArrayList<AreaTree>();
		List<City> citys = cityDao.listAllCity(null);
		citys.forEach(city -> {
			AreaTree tree= new AreaTree();
			BeanUtils.copyProperties(city,tree);
			tree.setId(city.getId()+"");
			if (city.getParentId() == null) {
				tree.setParentId("0");
			} else {
				tree.setParentId(city.getParentId() + "");

			}
			tree.setCode(city.getNodeCode()+"");
			rootNode.add(tree);
		});
		TreeUtil.appendChildren(root, rootNode);
		return root;
	}

	@Override
	public List<City> listAllCity(String cityName) {
		return this.cityDao.listAllCity(cityName);
	}
}
